//
//  YMGenerateQrcodeController.m
//  QrCode
//
//  Created by Andrew on 2018/6/25.
//  Copyright © 2018年 Andrew. All rights reserved.
//

#import "YMGenerateQrcodeController.h"
#import <CoreImage/CoreImage.h>
#import "CIImage+Extension.h"

#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height

@interface YMGenerateQrcodeController ()

/* 二维码 */
@property (nonatomic, weak) UIImageView *image;
/* 头像 */
@property (nonatomic, weak) UIImageView *photo;



@end

@implementation YMGenerateQrcodeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupView];
    
    [self creatQrcode];
}

- (void)setupView
{
    
    CGFloat width = (SCREENWIDTH - 40) / 2;
    CGFloat x = SCREENWIDTH / 2 - width / 2;
    CGFloat y = SCREENHEIGHT / 2 - width / 2;
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, width)];
    [self.view addSubview:image];
    self.image = image;
    
    //二维码中间的头像，在中间添加一个头像，不能太大
    CGFloat phtotWH = 40;
    CGFloat px = width / 2 - phtotWH / 2;
    UIImageView *photo = [[UIImageView alloc] initWithFrame:CGRectMake(px, px, phtotWH, phtotWH)];
    photo.image = [UIImage imageNamed:@"photo.jpg"];
    [self.image addSubview:photo];
    self.photo = photo;
}

- (void)creatQrcode
{
    //1、创建滤镜对象
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    //2、恢复默认设置
    [filter setDefaults];
    
    //3、设置数据
    NSString *info = @"http://www.baidu.com";
    NSData *infoData = [info dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:infoData forKey:@"inputMessage"];
    
    //4、生成二维码
    CIImage *outputImage = [filter outputImage];
    self.image.image = [outputImage ym_createNonInterpolatedWithSize:200];
}



@end
